package gui.newWindows;

import gui.Broadcaster;
import gui.GUISender;
import gui.Main.GUI;
import hypeerweb.Node;

import java.awt.GridLayout;
import java.awt.event.*;

import javax.swing.*;

public class SendWindowPanel extends JPanel
{
    protected GUI main;
    protected JLabel startingNodeLabel;
    protected JLabel endingNodeLabel;
    protected JLabel messageBoxLabel;
    protected JTextField startingNode;
    protected JTextField endingNode;
    protected JTextField messageBox;
    protected JButton sendButton;

    public SendWindowPanel(GUI main)
    {
        // super(new GridBagLayout());
        super(new GridLayout(3, 1));
        this.main = main;

        startingNodeLabel = new JLabel("Starting Node");
        endingNodeLabel = new JLabel("Ending Node");
        messageBoxLabel = new JLabel("Message");

        startingNode = new JTextField(3);
        endingNode = new JTextField(3);
        messageBox = new JTextField(20);

        // Build the send button
        sendButton = new JButton("Send Message");
        sendButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                sendButtonPressed();
            }

            public void windowClosing(WindowEvent we)
            {
                setSendWindowToNull();
            }
        });

        JPanel startingEndingNodePanel = new JPanel();
        startingEndingNodePanel.add(startingNodeLabel);
        startingEndingNodePanel.add(startingNode);
        startingEndingNodePanel.add(endingNodeLabel);
        startingEndingNodePanel.add(endingNode);
        this.add(startingEndingNodePanel);

        JPanel messageNodePanel = new JPanel();
        messageNodePanel.add(messageBoxLabel);
        messageNodePanel.add(messageBox);
        this.add(messageNodePanel);

        this.add(sendButton);

    }

    private void setSendWindowToNull()
    {
        main.getHyPeerWebDebugger().getStandardCommands().setSendWindowToNull();
    }

    private void sendButtonPressed()
    {
        int val1 = -1;
        int val2 = -1;
        main.getHyPeerWebDebugger().getStatus().setContent("");
        try
        {
            val1 = Integer.parseInt(startingNode.getText());
            val2 = Integer.parseInt(endingNode.getText());
        }
        catch (Exception e)
        {
            main.getHyPeerWebDebugger().getStatus().setContent("Error:  none Integer value found in start or end node box");
            return;
        }

        boolean canSendFrom = main.getHyPeerWeb().getNode(val1) != null;
        boolean canSendTo = main.getHyPeerWeb().getNode(val2) != null;
        //main.getHyPeerWeb().getNode(startId) != null
        //GUI.getSingleton(null).getHyPeerWeb().contains(val1)
        
        if(!canSendFrom)
        {
            main.getHyPeerWebDebugger().getStatus().setContent("Error, start node not found"); 
            return;
        }
        if(!canSendTo)
        {
            main.getHyPeerWebDebugger().getStatus().setContent("Error, end node not found"); 
            return;
        }
        GUISender s = new GUISender();
        Node node = main.getHyPeerWeb().getNode(val1);
        node.accept(s, GUISender.createInitialParameters(val2,messageBox.getText()));
    }
}
